//	CP2013 Traffic Simulator Assignment
//      Traffic Model Main Interface
//	Java version
//      Created in the NetBeans Interface Designer
//      Event Listeners coded by Matthew Johnson

package traffic;

import javax.swing.JSlider;

/**
 *
 * @author Matthew
 */
public class TrafficInterface extends javax.swing.JFrame {

    //The Controller
    private Controller c;

    /** Creates new form TrafficInterface */
    public TrafficInterface() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        optionPanel = new javax.swing.JPanel();
        optionPanelTop = new javax.swing.JPanel();
        HLanePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        hLaneSlider = new javax.swing.JSlider();
        VerticalLanePanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        vLaneSlider = new javax.swing.JSlider();
        jLabel3 = new javax.swing.JLabel();
        hProbSlider = new javax.swing.JSlider();
        hProbLabel = new javax.swing.JLabel();
        vProbLabel = new javax.swing.JLabel();
        vProbSlider = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cycleSlider = new javax.swing.JSlider();
        noCycleLabel = new javax.swing.JLabel();
        startButton = new javax.swing.JButton();
        graphicPanel = new GraphicPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Traffic Simulator");
        setMinimumSize(new java.awt.Dimension(500, 700));
        setName("Traffic Simulator"); // NOI18N
        setResizable(false);

        optionPanel.setPreferredSize(new java.awt.Dimension(480, 180));

        HLanePanel.setPreferredSize(new java.awt.Dimension(240, 100));

        jLabel1.setText("Horizontal Lanes:");

        hLaneSlider.setMajorTickSpacing(1);
        hLaneSlider.setMaximum(3);
        hLaneSlider.setMinimum(1);
        hLaneSlider.setPaintLabels(true);
        hLaneSlider.setSnapToTicks(true);
        hLaneSlider.setValue(2);
        hLaneSlider.setPreferredSize(new java.awt.Dimension(150, 37));

        javax.swing.GroupLayout HLanePanelLayout = new javax.swing.GroupLayout(HLanePanel);
        HLanePanel.setLayout(HLanePanelLayout);
        HLanePanelLayout.setHorizontalGroup(
            HLanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, HLanePanelLayout.createSequentialGroup()
                .addContainerGap(98, Short.MAX_VALUE)
                .addComponent(hLaneSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(HLanePanelLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                .addGap(88, 88, 88))
        );
        HLanePanelLayout.setVerticalGroup(
            HLanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(hLaneSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
        );

        jLabel2.setText("Vertical Lanes:");

        vLaneSlider.setMajorTickSpacing(1);
        vLaneSlider.setMaximum(4);
        vLaneSlider.setMinimum(1);
        vLaneSlider.setPaintLabels(true);
        vLaneSlider.setSnapToTicks(true);
        vLaneSlider.setValue(2);
        vLaneSlider.setPreferredSize(new java.awt.Dimension(150, 37));

        javax.swing.GroupLayout VerticalLanePanelLayout = new javax.swing.GroupLayout(VerticalLanePanel);
        VerticalLanePanel.setLayout(VerticalLanePanelLayout);
        VerticalLanePanelLayout.setHorizontalGroup(
            VerticalLanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, VerticalLanePanelLayout.createSequentialGroup()
                .addContainerGap(96, Short.MAX_VALUE)
                .addComponent(vLaneSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(VerticalLanePanelLayout.createSequentialGroup()
                .addComponent(jLabel2)
                .addContainerGap(176, Short.MAX_VALUE))
        );
        VerticalLanePanelLayout.setVerticalGroup(
            VerticalLanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(vLaneSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout optionPanelTopLayout = new javax.swing.GroupLayout(optionPanelTop);
        optionPanelTop.setLayout(optionPanelTopLayout);
        optionPanelTopLayout.setHorizontalGroup(
            optionPanelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelTopLayout.createSequentialGroup()
                .addComponent(HLanePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(VerticalLanePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        optionPanelTopLayout.setVerticalGroup(
            optionPanelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(HLanePanel, 0, 37, Short.MAX_VALUE)
            .addComponent(VerticalLanePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jLabel3.setText("Horizontal Probability:");

        hProbSlider.setMajorTickSpacing(25);
        hProbSlider.setMinorTickSpacing(1);
        hProbSlider.setSnapToTicks(true);
        hProbSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                hProbSliderStateChanged(evt);
            }
        });

        hProbLabel.setText("50");
        hProbLabel.setPreferredSize(new java.awt.Dimension(12, 20));

        vProbLabel.setText("50");
        vProbLabel.setPreferredSize(new java.awt.Dimension(12, 20));

        vProbSlider.setMajorTickSpacing(25);
        vProbSlider.setMinorTickSpacing(1);
        vProbSlider.setSnapToTicks(true);
        vProbSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                vProbSliderStateChanged(evt);
            }
        });

        jLabel4.setText("Vertical Probability:");

        jLabel5.setText("Number of Cycles:");

        cycleSlider.setMajorTickSpacing(25);
        cycleSlider.setMinimum(10);
        cycleSlider.setMinorTickSpacing(1);
        cycleSlider.setSnapToTicks(true);
        cycleSlider.setValue(10);
        cycleSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cycleSliderStateChanged(evt);
            }
        });

        noCycleLabel.setText("10");
        noCycleLabel.setPreferredSize(new java.awt.Dimension(12, 20));

        startButton.setText("Start");
        startButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                startButtonMouseClicked(evt);
            }
        });
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout optionPanelLayout = new javax.swing.GroupLayout(optionPanel);
        optionPanel.setLayout(optionPanelLayout);
        optionPanelLayout.setHorizontalGroup(
            optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionPanelTop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(optionPanelLayout.createSequentialGroup()
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cycleSlider, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addComponent(vProbSlider, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addComponent(hProbSlider, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(hProbLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vProbLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noCycleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        optionPanelLayout.setVerticalGroup(
            optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelLayout.createSequentialGroup()
                .addComponent(optionPanelTop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(hProbSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(hProbLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vProbSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(vProbLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(noCycleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(cycleSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        graphicPanel.setMinimumSize(new java.awt.Dimension(490, 490));
        graphicPanel.setPreferredSize(new java.awt.Dimension(490, 490));
        graphicPanel.setRequestFocusEnabled(false);

        javax.swing.GroupLayout graphicPanelLayout = new javax.swing.GroupLayout(graphicPanel);
        graphicPanel.setLayout(graphicPanelLayout);
        graphicPanelLayout.setHorizontalGroup(
            graphicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 490, Short.MAX_VALUE)
        );
        graphicPanelLayout.setVerticalGroup(
            graphicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 492, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(graphicPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(optionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(graphicPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Event handler for a change in the Horizontal Probability Slider's value
    private void hProbSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_hProbSliderStateChanged
        //Get the slider that caused the event
        JSlider source = (JSlider)evt.getSource();
        //And output its value to the Slider's value label
        hProbLabel.setText("" + source.getValue());  
    }//GEN-LAST:event_hProbSliderStateChanged
    //Event handler for a change in the Vertical Probability Slider's value
    private void vProbSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_vProbSliderStateChanged
        //Get the slider that caused the event
        JSlider source = (JSlider)evt.getSource();
        //And output its value to the Slider's value label
        vProbLabel.setText("" + source.getValue()); 
    }//GEN-LAST:event_vProbSliderStateChanged
    //Event handler for a change in the value of the set number of cycles slider
    private void cycleSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cycleSliderStateChanged
        //Get the slider that caused the event
        JSlider source = (JSlider)evt.getSource();
        //And output its value to the Slider's value label
        noCycleLabel.setText("" + source.getValue());
    }//GEN-LAST:event_cycleSliderStateChanged

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_startButtonActionPerformed
    //Event handler for the user clicking the start button
    private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startButtonMouseClicked
        //If an existing simulation is running, we need to stop it
        if (c != null && c.isAlive())
        {
            c.stop();
        }

        //Get the user's settings for the simulation
        SimulationParams settings = new SimulationParams();
        settings.ewLanes = hLaneSlider.getValue();
        settings.nsLanes = vLaneSlider.getValue();
        settings.ewProb = hProbSlider.getValue();
        settings.nsProb = vProbSlider.getValue();
        settings.numCycles = cycleSlider.getValue();

        //Setup the model
        Model m = new Model(settings);

        //Setup the visualisation panel
        ((GraphicPanel)graphicPanel).setModel(m);

        //Setup the controller
       c = new Controller(m, (GraphicPanel)graphicPanel);

        //Run the cycles
        c.start();

    }//GEN-LAST:event_startButtonMouseClicked

    //Main Function: Starts the simulator
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TrafficInterface().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel HLanePanel;
    private javax.swing.JPanel VerticalLanePanel;
    private javax.swing.JSlider cycleSlider;
    private javax.swing.JPanel graphicPanel;
    private javax.swing.JSlider hLaneSlider;
    private javax.swing.JLabel hProbLabel;
    private javax.swing.JSlider hProbSlider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel noCycleLabel;
    private javax.swing.JPanel optionPanel;
    private javax.swing.JPanel optionPanelTop;
    private javax.swing.JButton startButton;
    private javax.swing.JSlider vLaneSlider;
    private javax.swing.JLabel vProbLabel;
    private javax.swing.JSlider vProbSlider;
    // End of variables declaration//GEN-END:variables

}
